import { Component, OnInit } from '@angular/core';
import { HttpService } from '../../service/http.service';
import { HttpRouterService } from '../../service/http-router.service';
import { StorageService } from '../../service/storage.service';
import { Router } from '@angular/router';
import { CityData } from '../../city.data';
import _ from 'underscore';

@Component({
  selector: 'app-product',
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.less']
})
export class ProductComponent implements OnInit {
  public productList = [];
  public _cityList = [];
  public flags = {
    hot:"热门",
    fenqi:"现金分期",
    card:"还信用卡",
    sesame:"芝麻信用",
    real:"实名认证"
  }
  public money = "";
  public province = "";
  public loantype = "";
  public isShow = false;
  public showProvince = "全国";

  constructor(
    private _http:HttpService,
    private _router: Router,
    private _storage: StorageService,
    private _httpRouter:HttpRouterService
  ) { }

  ngOnInit() {
    this._cityList = CityData;
    this.getPrductList();
  }

  getPrductList(){
    let param = "money="+this.money+"&province="+this.province+"&loantype="+this.loantype;
    this._http.post(this._httpRouter.getProductList,param).then(res => {
      if(res.code == 1){
        this.productList = res.data;
        _.map(this.productList,item => {
          item.flag = item.flag.split(",");
        })
        this.productList = _.sortBy(this.productList, item => {
          return item.rank;
        });
      }
    });
  }

  getProduct(productId){
    this._storage.setSession("productId",productId);
    this._router.navigate(['/member/prodcutDetails']);
  }
  showChoose(){
    this.isShow = true;
  }
  checkProvince(id="",name="全国"){
    this.province = id;
    this.showProvince = name;
    this.isShow = false;
    this.getPrductList();
  }

  setOrder(pid,outerurl){
    let user = this._storage.getSessionObject("customer");
    let param = "pid="+pid+"&uid="+user.id;
    this._http.post(this._httpRouter.setOrder,param).then(res => {
      window.location.href = outerurl;
    });
  }

  download(){
    this._router.navigate(['/download']);
  }
}
